package com.estar.assistsurvy.activity;

import java.util.Timer;
import java.util.TimerTask;

import com.estar.assistsurvy.R;
import com.estar.assistsurvy.R.color;
import com.estar.assistsurvy.entity.RequestMsg;
import com.estar.assistsurvy.entity.RequestRgt;
import com.estar.assistsurvy.entity.ResponseMsg;
import com.estar.assistsurvy.entity.ResponseRgt;
import com.estar.assistsurvy.entity.TSRole;
import com.estar.assistsurvy.entity.User;
import com.estar.assistsurvy.thread.GetCodeThread;
import com.estar.assistsurvy.thread.RegisterThread;
import com.estar.assistsurvy.utils.CheckInfoUtils;
import com.estar.assistsurvy.utils.MD5Utils;
import com.estar.assistsurvy.utils.MightypleUtil;
import com.estar.assistsurvy.utils.SharepreferenceUtils;
import com.estar.assistsurvy.utils.ValidatorUtils;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class RegistActivity extends BaseActivity implements OnClickListener{
	private LinearLayout returnBack;
	private Button registPage_getCode,registPage_commit;
	private EditText registPage_input_tel,registPage_input_code,
	registPage_input_pwd,registPage_refereePhoneNumber;
	private ProgressDialog dialog;
	private Intent intent;
	private Timer timer;
	private int timeOut=60;
	private Handler handler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			if(dialog!=null){
				dialog.dismiss();
			}
			switch (msg.what) {
			case -1:
				showShortToast((String) msg.obj);
				break;
			case 0:
				ResponseMsg message=new ResponseMsg();
				message=(ResponseMsg) msg.obj;
				if(message.getCode().equals("1")&&timer==null && timeOut>0){
					timer=new Timer();
					timer.schedule(new MyTimerTask(), 0, 1000);
				}
				showShortToast(message.getMessages());
				break;

			case 1:
				ResponseRgt register=new ResponseRgt();
				register=(ResponseRgt) msg.obj;
				if(register.getCode().equals("1")){
					User user=register.getUser();
					SharepreferenceUtils.saveUser(user);
					
					TSRole role=register.getRole();
					if(role!=null&&!role.equals("")){
						SharepreferenceUtils.saveRole(role);
					}
					intent=new Intent(RegistActivity.this, MainActivity.class);
					startActivity(intent);
					finish();
				}
				showShortToast(register.getMessages());
				break;
				
			case 2:
				registPage_getCode.setText(timeOut+"s");
				break;
				
			case 3:
				registPage_getCode.setText(R.string.get_checkCode);
				break;

			default:
				break;
			}

		};
	};

	private String fromType="0",deviceModel="1",refereePhoneNumber="";
	private String deviceId,phoneNumber,password,code;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_regist);
		initView();

		registPage_input_tel.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
			
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				phoneNumber=registPage_input_tel.getText().toString();
//				if(new CheckInfoUtils().isPhoneNumberValid(phoneNumber)){
					if(registPage_input_tel.getText().toString().trim().length()>=11){
						registPage_getCode.setClickable(true);
						registPage_getCode.setTextColor(getResources().getColor(color.colorTextOrange));
					}else{
						registPage_getCode.setClickable(false);
						registPage_getCode.setTextColor(color.colorTextGrey);
					}
//				}else{
//					Toast.makeText(RegistActivity.this, "手机号码格式不正确", 2000).show();
//				}
				
			}
		});
	}

	private void initView(){
		registPage_input_tel=find(R.id.registPage_input_tel);

		registPage_input_code=find(R.id.registPage_input_code);

		registPage_input_pwd=find(R.id.registPage_input_pwd);

		registPage_refereePhoneNumber=find(R.id.registPage_refereePhoneNumber);

		registPage_getCode=find(R.id.registPage_getCode);
		registPage_getCode.setOnClickListener(this);

		registPage_commit=find(R.id.registPage_commit);
		registPage_commit.setOnClickListener(this);

		returnBack=find(R.id.returnBack);
		returnBack.setOnClickListener(this);

		deviceId=MightypleUtil.getDeviceId(getApplicationContext());
		
	}

	private class MyTimerTask extends TimerTask{

		@Override
		public void run() {
			// TODO Auto-generated method stub
			timeOut--;
			if(timeOut==0){
				timeOut=60;
				handler.sendEmptyMessage(3);
				timer.cancel();
				timer=null;
			}else{
				handler.sendEmptyMessage(2);
			}
			
		}

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.registPage_getCode:
			phoneNumber=registPage_input_tel.getText().toString();
			if(new CheckInfoUtils().isPhoneNumberValid(phoneNumber)){
				RequestMsg requestMsg=new RequestMsg("",deviceId, phoneNumber, fromType);
				new GetCodeThread(RegistActivity.this, handler, requestMsg).start();
			}else{
				showShortToast("手机号码格式不正确!");
			}

			break;

		case R.id.registPage_commit:
			if(!MightypleUtil.isEmpty(registPage_refereePhoneNumber.getText().toString())){
				refereePhoneNumber=registPage_refereePhoneNumber.getText().toString().trim();
				if(ValidatorUtils.isMobile(refereePhoneNumber)==Boolean.FALSE){
					showShortToast("关联手机号码格式不正确!");
				}
			}
			
			password=registPage_input_pwd.getText().toString().trim();
			code=registPage_input_code.getText().toString().trim();
			phoneNumber=registPage_input_tel.getText().toString().trim();
			if(!MightypleUtil.isEmpty(phoneNumber) && !MightypleUtil.isEmpty(code)&& !MightypleUtil.isEmpty(password)){
				dialog=ProgressDialog.show(this, null, "注册信息提交…", false, true);

				RequestRgt requestRgt=new RequestRgt("",deviceId, phoneNumber, MD5Utils.getMD5(password), deviceModel, code, refereePhoneNumber);
				new RegisterThread(RegistActivity.this, handler, requestRgt).start();
						
			}else{
				showShortToast("请将注册信息填写完整!");
			}
			break;

		case R.id.returnBack:
			finish();
			break;

		default:
			break;
		}
	}
	

}
